package com.glory.dgcpcpda.activity.mfg;

import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.recyclerview.widget.RecyclerView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.glory.dgcpcpda.R;
import com.glory.dgcpcpda.adapter.EqpKeepCheckListAdapter;
import com.glory.dgcpcpda.adapter.EqpKeepInputListAdapter;
import com.glory.dgcpcpda.base.BaseActivity;
import com.glory.dgcpcpda.entity.EqpKeepBean;
import com.glory.dgcpcpda.entity.EqpKeepSelectorListBean;
import com.glory.dgcpcpda.entity.response.BaseResponseBean;
import com.glory.dgcpcpda.utils.NetUtils;
import com.google.gson.Gson;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import com.google.gson.reflect.TypeToken;
import com.lzy.okgo.model.Response;
import com.pda.platform.ui.ui_pdaplatform.callback.FreeUI_DialogEntityCallBack;
import com.pda.platform.ui.ui_pdaplatform.callback.FreeUI_EntityCallBack;
import com.pda.platform.ui.ui_pdaplatform.utils_public.FreeApi_StringUtils;

import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

public class EqpQcDescActivity extends BaseActivity {

    @BindView(R.id.rvKeepCheckList)
    RecyclerView rvKeepCheckList;
    @BindView(R.id.rvKeepInputList)
    RecyclerView rvKeepInputList;
    @BindView(R.id.llMain)
    LinearLayout llMain;
    @BindView(R.id.llCheckLayout)
    LinearLayout llCheckLayout;
    @BindView(R.id.llInputLayout)
    LinearLayout llInputLayout;
    @BindView(R.id.tvTitle)
    TextView tvTitle;
    @BindView(R.id.tvBaseSubmit)
    TextView tvBaseSubmit;
    @BindView(R.id.etComment)
    EditText etComment;

    private String scheduleRrn;
    private String name;
    private EqpKeepCheckListAdapter eqpKeepCheckListAdapter;
    private EqpKeepInputListAdapter eqpKeepInputListAdapter;
    private List<EqpKeepSelectorListBean> checkTableLineDatas;

    @Override
    protected int getLayoutResId() {
        return R.layout.activity_eqp_qc_desc;
    }

    @Override
    protected void initView() {
        setMainLayout(llMain);
        tvTitle.setText("设备QC");
        tvBaseSubmit.setVisibility(View.GONE);

        initRecyclerView(rvKeepCheckList,this);
        initRecyclerView(rvKeepInputList,this);

        Bundle bundle = getIntent().getExtras();
        scheduleRrn = bundle.getString("scheduleRrn");
        name = bundle.getString("name");

        eqpKeepCheckListAdapter = new EqpKeepCheckListAdapter(null,false);
        eqpKeepCheckListAdapter.openLoadAnimation(BaseQuickAdapter.ALPHAIN);
        eqpKeepCheckListAdapter.isFirstOnly(true);
        rvKeepCheckList.setAdapter(eqpKeepCheckListAdapter);

        eqpKeepInputListAdapter = new EqpKeepInputListAdapter(null,false);
        eqpKeepInputListAdapter.openLoadAnimation(BaseQuickAdapter.ALPHAIN);
        eqpKeepInputListAdapter.isFirstOnly(true);
        rvKeepInputList.setAdapter(eqpKeepInputListAdapter);

    }

    @Override
    protected void initData() {

        FreeUI_EntityCallBack<BaseResponseBean<EqpKeepBean>> callBack = new FreeUI_DialogEntityCallBack<BaseResponseBean<EqpKeepBean>>
                (new TypeToken<BaseResponseBean<EqpKeepBean>>() {
                }.getType(), getSupportFragmentManager(), this) {

            @Override
            public void onSuccess(Response<BaseResponseBean<EqpKeepBean>> response) {
                super.onSuccess(response);
                if (response.body().isSuccess(getApplicationContext(), true, getSupportFragmentManager())) {
                    EqpKeepBean eqpKeepBean = response.body().getResponse().getBody();
                    if (eqpKeepBean.getCheckTableLineDatas().size() > 0) {
                        llCheckLayout.setVisibility(View.VISIBLE);
                        checkTableLineDatas = eqpKeepBean.getCheckTableLineDatas();
                        eqpKeepCheckListAdapter.setNewData(checkTableLineDatas);
                    }

                    if (eqpKeepBean.getLineDatas().size() > 0) {
                        llInputLayout.setVisibility(View.VISIBLE);
                        eqpKeepInputListAdapter.setNewData(eqpKeepBean.getLineDatas());
                    }
                }
            }

            @Override
            public void onError(Response<BaseResponseBean<EqpKeepBean>> response) {
                super.onError(response);
                loadError(response.getException(), "PMSSCHEDULE");
            }
        };
        JsonObject map = new JsonObject();
        map.addProperty("actionType", "queryScheduleForm");
        map.addProperty("scheduleRrn", scheduleRrn);
        map.addProperty("yn", "Y");
        NetUtils.mRequestPostNet("CPC.PMSSCHEDULE", name,"PMSSCHEDULE", this, map, callBack);

    }

    @OnClick({R.id.tvReplay, R.id.tvSave})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.tvSave:
                if (getText(etComment).equals("")){
                    showToast("请输入备注",false);
                    return;
                }
                showSubmitDialog(new onSubmitListener() {
                    @Override
                    public void setOnSubmitListener() {
                        submit("true","QC_PM_END");
                    }
                });
                break;
            case R.id.tvReplay:
                if (getText(etComment).equals("")){
                    showToast("请输入备注",false);
                    return;
                }
                showSubmitDialog(new onSubmitListener() {
                    @Override
                    public void setOnSubmitListener() {
                        if (eqpKeepInputListAdapter.getOutData().size() > 0) {
                            showConfirmDialog("数值已超限");
                            return;
                        }

                        List<EqpKeepSelectorListBean> data = eqpKeepCheckListAdapter.getData();
                        for (EqpKeepSelectorListBean bean : data) {
                            if (FreeApi_StringUtils.isEmpty(bean.getReserved1())) {
                                showToast("保养状态项不能为空", false);
                                return;
                            }

//                            if (FreeApi_StringUtils.isEmpty(bean.getReserved9()) && bean.getReserved10().length() > 0 && !bean.getReserved10().contains(";")) {
//                                showToast("QC采集项未完成", false);
//                                return;
//                            }
                        }
                        submit("true","QC_PM");
                    }
                },"确认提交？");
                break;
        }
    }

    private void submit(String isFinished,String qc) {

        FreeUI_EntityCallBack<BaseResponseBean<EqpKeepBean>> callBack = new FreeUI_DialogEntityCallBack<BaseResponseBean<EqpKeepBean>>
                (new TypeToken<BaseResponseBean<EqpKeepBean>>() {
                }.getType(), getSupportFragmentManager(), this) {

            @Override
            public void onSuccess(Response<BaseResponseBean<EqpKeepBean>> response) {
                super.onSuccess(response);
                if (response.body().isSuccess(getApplicationContext(), false, getSupportFragmentManager())) {
                    showConfirmDialog(EqpQcDescActivity.this, "操作成功");
                }
            }

            @Override
            public void onError(Response<BaseResponseBean<EqpKeepBean>> response) {
                super.onError(response);
                loadError(response.getException(), "PMSSCHEDULE");
            }
        };

        JsonObject map = new JsonObject();
//        JsonObject checkJson = new JsonObject();
//        checkJson.add("XPdaPmsSheetCheckTableLineData", new JsonParser().parse(new Gson().toJson(eqpKeepCheckListAdapter.getData())).getAsJsonArray());
//        JsonObject inputJson = new JsonObject();
//        inputJson.add("XPdaPmsDatasLineData", new JsonParser().parse(new Gson().toJson(eqpKeepInputListAdapter.getData())).getAsJsonArray());
        map.addProperty("actionType", "saveScheduleForm");
        map.addProperty("isFinished", isFinished);
        map.addProperty("QC",qc);
        map.addProperty("COMMENTS",etComment.getText().toString());
        map.add("XPdaPmsDatasLineDatas", new JsonParser().parse(new Gson().toJson(eqpKeepInputListAdapter.getData())).getAsJsonArray());
        map.add("XPdaPmsSheetCheckTableLineDatas", new JsonParser().parse(new Gson().toJson(eqpKeepCheckListAdapter.getData())));
        map.addProperty("scheduleRrn", scheduleRrn);

        NetUtils.mRequestPostNet("CPC.PMSSCHEDULE",name,"PMSSCHEDULE", this, map, callBack);

    }
}